package com.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xq.dto.SysDict.SysDictDataInsertDTO;
import com.xq.dto.SysDict.SysDictDataListDTO;
import com.xq.dto.SysDict.SysDictDataUpdateDTO;
import com.xq.entity.SysDictData;
import com.xq.exception.BusinessException;
import com.xq.service.impl.msMapper.MsSysDictDataMapper;
import com.xq.mapper.SysDictDataMapper;
import com.xq.service.ISysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.vo.DataBaseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author wuven
 * @since 2025-05-28
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private MsSysDictDataMapper msSysDictDataMapper;

    @Override
    public void insertDictData(SysDictDataInsertDTO sysDictDataInsertDTO) {
        SysDictData sysDictData = msSysDictDataMapper.sysDictDataInsertDtoToSysDictData(sysDictDataInsertDTO);
        int insert = sysDictDataMapper.insert(sysDictData);
        if (insert <=0) {
            throw new BusinessException(DataBaseEnum.INSERTION_FAILED);
        }
    }

    @Override
    public void deleteDictData(String dictCode) {
        int i = sysDictDataMapper.deleteById(dictCode);
        if (i <= 0) {
            throw new BusinessException(DataBaseEnum.DELETE_FAILED);
        }
    }

    @Override
    public void updateDictData(SysDictDataUpdateDTO sysDictDataUpdateDTO) {
        SysDictData sysDictData = msSysDictDataMapper.sysDictDataUpdateDtoToSysDictData(sysDictDataUpdateDTO);
        int update = sysDictDataMapper.updateById(sysDictData);
        if (update <= 0) {
            throw new BusinessException(DataBaseEnum.UPDATE_FAILED);
        }
    }

    @Override
    public List<SysDictDataListDTO> listDictData(String typeCode) {
        QueryWrapper<SysDictData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type_code", typeCode);
        List<SysDictData> sysDictDataList=sysDictDataMapper.selectList(queryWrapper);
        List<SysDictDataListDTO> sysDictDataListDTOS=new ArrayList<>();
        for (SysDictData sysDictData : sysDictDataList) {
            SysDictDataListDTO sysDictDataListDTO = msSysDictDataMapper.sysDictDataToSysDictDataListDTO(sysDictData);
            sysDictDataListDTOS.add(sysDictDataListDTO);
        }

        return sysDictDataListDTOS;
    }
}
